﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.Repository.Interface;

namespace Bymed.SpineScan.UnitTest.FakeRepository
{
    public class FakeRepositoryBase<T> : IRepository<T> where T : class
    {
        protected static List<T> DataList;

        public FakeRepositoryBase()
        {
            GenerateInMemoryData();
        }

        public object Add(T entity)
        {
            DataList.Add(entity);
            return entity;
        }

        public T Get(object id)
        {
            //return DataList.Where(o => o.ID == (int)id).FirstOrDefault();
            return DataList[0];
        }

        public T Load(object id)
        {
            return Get(id);
        }

        public IQueryable<T> Query(Expression<Func<T, bool>> expression)
        {
            return DataList.AsQueryable().Where(expression).AsQueryable();
        }

        public IQueryable<T> Query(Expression<Func<T, bool>> expression, string includePath)
        {
            return DataList.AsQueryable().Where(expression).AsQueryable();
        }

        public IQueryable<TResult> Query<TResult>(Expression<Func<T, bool>> expression, Expression<Func<T, TResult>> selector)
        {
            return null;
        }

        public IQueryable<TResult> Query<TResult>(Expression<Func<T, bool>> expression, Expression<Func<T, TResult>> selector, string includePath)
        {
            return null;
        }

        public IQueryable<T> GetAll()
        {
            return DataList.AsQueryable();
        }

        public int GetRowCount(Expression<Func<T, bool>> expression)
        {
            return DataList.AsQueryable().Where(expression).Count();
        }

        public void Update(T entity)
        {
            //T t = DataList.Where(o => o.ID == (int)entity.ID).FirstOrDefault();

            //if (t != null)
            //{
            //    int index = DataList.IndexOf(t);
            //    DataList.RemoveAt(index);
            //    DataList.Insert(index, entity);
            //}
        }

        public void AddOrUpdate(T entity)
        {
            //T t = Get(entity.ID);

            //if (t == null)
            //{
            //    DataList.Add(entity);
            //}
            //else
            //{
            //    int index = DataList.IndexOf(t);
            //    DataList.RemoveAt(index);
            //    DataList.Insert(index, entity);
            //}

        }

        public void Delete(T entity)
        {
            //T t = DataList.Where(o => o.ID == (int)entity.ID).FirstOrDefault();

            //if (t != null)
            //{
            //    int index = DataList.IndexOf(t);
            //    DataList.RemoveAt(index);
            //}
        }

        protected virtual void GenerateInMemoryData() { }


        public void ExecuteSqlCommand(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        T IRepository<T>.Add(T entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<T> Add(IEnumerable<T> entities)
        {
            throw new NotImplementedException();
        }

        public void Update(IEnumerable<T> entities)
        {
            throw new NotImplementedException();
        }

        public void AddOrUpdate(IEnumerable<T> entities)
        {
            throw new NotImplementedException();
        }

        public void Delete(IEnumerable<T> entities)
        {
            throw new NotImplementedException();
        }


        public IQueryable<T> GetAll(string includePath)
        {
            throw new NotImplementedException();
        }
    }
}
